package top.xunfong.knowledge.controller;

import org.springframework.web.multipart.MultipartFile;
import top.xunfong.common.core.domain.R;
import top.xunfong.common.core.domain.TableDataInfo;
import top.xunfong.common.core.domain.dto.BatchDTO;
import top.xunfong.knowledge.dto.*;
import top.xunfong.knowledge.service.KnowledgeFileService;
import top.xunfong.knowledge.service.MultipartService;
import top.xunfong.knowledge.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.xunfong.mybatis.core.page.PageDTO;

@RestController
@RequestMapping("/knowledgeFile")
public class KnowledgeFileController {

    @Autowired
    private KnowledgeFileService knowledgeFileService;

    @Autowired
    private MultipartService multipartService;
    /**
     * 新增
     *
     * @param dto
     * @return
     */
    @PostMapping("/insert")
    public R<?> insertKnowledgeFile(@RequestBody KnowledgeFileDTO dto) {
        return knowledgeFileService.insertKnowledgeFile(dto);
    }

    /**
     * 初始化分片上传
     *
     * @return
     */
    @PostMapping("/file/InitiateMultipartUpload")
    public R<InitiateMultipartUploadVO> fileInitiateMultipartUpload(@RequestBody InitiateMultipartUploadDTO dto) {
        return R.ok(multipartService.initiateMultipartUpload(dto));
    }

    /**
     * 上传分片信息
     *
     * @return
     */
    @PostMapping("/file/UploadPart")
    public R<UploadPartVO> fileUploadPart(@RequestBody UploadPartDTO dto) {
        return R.ok(multipartService.uploadPart(dto));
    }

    /**
     * 合并分片
     *
     * @return
     */
    @PostMapping("/file/CompleteMultipartUpload")
    public R<CompleteMultipartUploadVO> fileCompleteMultipartUpload(@RequestBody CompleteMultipartUploadDTO dto) {
        return R.ok(multipartService.completeMultipartUpload(dto));
    }

    /**
     * 取消分片上传
     *
     * @return
     */
    @PostMapping("/file/AbortMultipartUpload")
    public R<AbortMultipartUploadVO> fileAbortMultipartUpload(@RequestBody AbortMultipartUploadDTO dto) {
        return R.ok(multipartService.abortMultipartUpload(dto));
    }

    /**
     * 修改
     *
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public R<?> updateKnowledgeFile(@RequestBody KnowledgeFileDTO dto) {
        return knowledgeFileService.updateKnowledgeFile(dto);
    }

    /**
     * 批量删除
     *
     * @param dto
     * @return
     */
    @PostMapping("/delete")
    public R<?> deleteKnowledgeFile(@RequestBody BatchDTO<Long> dto) {
        return knowledgeFileService.deleteKnowledgeFile(dto);
    }

    /**
     * 列表查询
     *
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public TableDataInfo<KnowledgeFileVO> listKnowledgeFile(@RequestBody KnowledgeFileQueryDTO dto, PageDTO pageDTO) {
        return knowledgeFileService.listKnowledgeFile(dto, pageDTO);
    }

    @GetMapping("/single")
    public R<KnowledgeFileVO> singleKnowledgeFile(Long id) {
        return knowledgeFileService.singleKnowledgeFile(id);
    }
}